// ========================================================================
// Copyright (c) 2022 ChangYou, All rights reserved.
// http://www.changyou.com/
// 
// Filename:    FoliageHeightMap.cs
// Time:        2022-08-22 20:39:02
// Author:      xiangjinbao
// Email:       xiangjinbao@cyou-inc.com
// Version:     v2022.0.1
// Description: FoliageHeightMap
// ========================================================================

using UnityEngine;

namespace ProjectTW.Foliage
{
    /// <summary>
    /// Channels:
    /// R: Heights Channel #1
    /// G: Heights Channel #2
    /// B: Perlin Noise
    /// </summary>
    [System.Serializable]
    public class FoliageHeightMap : FoliageMapBase
    {
        /// <summary>
        /// 获取高度图路径
        /// </summary>
        /// <param name="foliageInstance"></param>
        /// <returns></returns>
        public static string GetPath(FoliageChunk foliageInstance)
        {
            return FoliageMapUtility.GetMapPath("Height", foliageInstance.AttachedFoliageInstance.Guid, "0");
        }

        public FoliageHeightMap(Texture2D texture, FoliageChunk foliageInstance) : base(texture, texture.GetPixels32(), foliageInstance)
        {
        }

        /// <summary>
        /// 保存高度图
        /// </summary>
        public void Save()
        {
            if (Dirty)
            {
#if UNITY_EDITOR
                FoliageMapUtility.SaveMap(this, GetPath(FoliageInstance));
#endif
                Dirty = false;
            }
        }

        /// <summary>
        /// 释放细节对象的高度图
        /// </summary>
        public override void Dispose()
        {
            FoliageMapUtility.DisposeMap(Map, GetPath(FoliageInstance));
        }

        /// <summary>
        /// 将某个索引中的高度快速设置为世界高度
        /// Index = x + z * res
        /// </summary>
        public void SetHeightFast(int index, Color32 colorInstance, byte h1, byte h2)
        {
            colorInstance.r = h1;
            colorInstance.g = h2;
            colorInstance.b = (byte) FoliageMathUtility.Random.GetRndRange(0, 255);

            MapPixels[index] = colorInstance;
        }
    }
}
